package com.example.demo.service.impl;

import com.example.demo.dao.SceneryOrderDao;
import com.example.demo.entity.SceneryOrder;
import com.example.demo.service.SceneryOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SceneryOrderServiceImpl implements SceneryOrderService {
    private SceneryOrderDao sceneryOrderDao;
    @Autowired
    public void setSceneryOrderDao(SceneryOrderDao sceneryOrderDao) {
        this.sceneryOrderDao = sceneryOrderDao;
    }

    @Override
    public int add(SceneryOrder sceneryOrder) {
        System.out.println(sceneryOrder.getUserId()+"=====");
        SceneryOrder s=sceneryOrderDao.find(sceneryOrder.getUserId(),sceneryOrder.getSceneryId(),sceneryOrder.getTime());
        if(s==null){
            return sceneryOrderDao.add(sceneryOrder);
        }
       else{

            s.setPeople(sceneryOrder.getPeople()+s.getPeople());
            return sceneryOrderDao.update(s);
        }
    }

    @Override
    public PageInfo<SceneryOrder> getPage(int pageNo, int pageSize) {
        //开始分页
        PageHelper.startPage(pageNo,pageSize) ;
        //获取所有数据
        List<SceneryOrder> list = sceneryOrderDao.getAll() ;
        //创建PageInfo对象
        PageInfo<SceneryOrder> pageInfo = new PageInfo<>(list) ;

        return pageInfo;
    }

    @Override
    public List<SceneryOrder> getOrderByUserId(int userId) {
        return sceneryOrderDao.getOrderByUserId(userId);
    }

    @Override
    public int delete(int id) {
        return sceneryOrderDao.delete(id);
    }
}
